package com.peng;

import java.awt.*;
import java.io.*;
import java.util.ArrayList;
import java.util.List;

/**
 * 门面模式 facade  将一系列游戏物体包装起来，  tankframe只需要通过访问gamemodel来去访问    gamemodel就是那个大管家
 * 调停者模式 Mediator   有一个中间者 来协调 内部的交流     消息中间件就是Mediator调停者模式
 * Facade门面-Mediator调停者可以看作是同一种设计模式
 */
public class GameModel implements Serializable {
    private Tank mainTank;
    private List<GameObject> objectList = new ArrayList<>();


    private ColliderChain colliderChain = new ColliderChain();

    public static GameModel INSTANCE = new GameModel();

    static {
        INSTANCE.init();
    }

    public GameModel() {

    }

    public static GameModel getInstance() {
        return INSTANCE;
    }

    private void init() {
        mainTank = new Tank(200, 200, Group.GOOD, false);
        int size = Integer.parseInt(PropertiesManager.getValue("BAD_GROUP_TANK_SIZE"));
        for (int i = 0; i < size; i++) {
            new Tank(20 + i * 70, 300, Group.BAD, true);
        }
        for (int i = 0; i < 5; i++) {
            new Wall(80 + 140 * i, 70 + 130 * i, 50, 100);
        }
    }

    public void add(GameObject gameObject) {
        this.objectList.add(gameObject);
    }


    public boolean remove(GameObject gameObject) {
        return this.objectList.remove(gameObject);
    }


    public void paint(Graphics g) {

        mainTank.paint(g); //绘制 主机坦克行动轨迹

        //绘制游戏物体
        for (int i = objectList.size() - 1; i >= 0; i--) {
            objectList.get(i).paint(g);
        }


        //检测碰撞
        for (int i = 0; i < objectList.size(); i++) {
            for (int j = i + 1; j < objectList.size(); j++) {

                GameObject gameObject = objectList.get(i);
                GameObject gameObject2 = objectList.get(j);
                colliderChain.collide(gameObject, gameObject2);

            }
        }


    }

    public Tank getMainTank() {
        return mainTank;
    }

    public void setMainTank(Tank tank) {
        this.mainTank = tank;
    }


    public void save() {
        ObjectOutputStream oos = null;
        try {
            FileOutputStream f = new FileOutputStream("save.data");
            oos = new ObjectOutputStream(f);
            oos.writeObject(this);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (oos != null) {
                try {
                    oos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }

            }
        }
    }

    public void load() {
        ObjectInputStream oos = null;
        try {
            FileInputStream f = new FileInputStream("save.data");
            oos = new ObjectInputStream(f);
            GameModel.INSTANCE = (GameModel) oos.readObject();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } finally {
            if (oos != null) {
                try {
                    oos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }

            }
        }
    }

}




